package in.muton.edushare.components.messaging.api;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.auth.api.Authentication;

import com.edushare.common.dao.OutgoingEmailsDAO;
import com.edushare.common.mailer.DeletedAssignment;
import com.edushare.common.mailer.EditedAssignment;
import com.edushare.common.mailer.NewAssignment;
import com.edushare.common.vo.Assignments;
import com.edushare.common.vo.Users;

public class AssignmentMessage extends Message {

	public AssignmentMessage(Assignments assignment, String notificationType) {
		setReciever(assignment.getCourseInstance());
		this.assgn = assignment;
		this.assgnType = notificationType;
	}

	@Override
	protected String getMessage() {
		if (assgnType.equals(ASSIGNMENT_CANCELLED))
			return "The assignment <strong>" + assgn.getAssignmentTitle()
					+ "</strong> has been cancelled.";
		return "<<<EDUSHAREMESSAGINGMETADATA:ASSIGNMENT:" + assgnType + ":"
				+ assgn.getObjectPrimaryKey() + ">>>";

	}

	private Assignments assgn = null;
	private String assgnType = "";
	public final static String NEW_ASSIGNMENT_CREATED = "NEWASSIGNMENT";
	public final static String ASSIGNMENT_TITLE_MODIFIED = "TITLEMODIFY";
	public final static String ASSIGNMENT_FILES_UPLOADED = "FILEUPLOAD";
	public final static String ASSIGNMENT_CANCELLED = "CANCELLED";
	public final static String ASSIGNMENT_DATES_CHANGED = "DATECHANGE";

	@Override
	protected void addditionalProcessing(Users u) {
		Users loggedIn = EduShareSession.get().getLoggedInUser();
		Authentication auth= new Authentication(u, EduShareSession.get().getLoggedInUser());
		if(!auth.getSetting(Authentication.NOTIFY_ASS).equals("t") || u==loggedIn)
			return;
			
		if (assgnType.equals(NEW_ASSIGNMENT_CREATED)) {
			new OutgoingEmailsDAO(EduShareSession.get()
					.getSessionORMInterface()).sendMail(new NewAssignment(
					EduShareSession.get().getLoggedInUser(), u, assgn).send());
		} else if (assgnType.equals(ASSIGNMENT_CANCELLED)) {
			new OutgoingEmailsDAO(EduShareSession.get()
					.getSessionORMInterface()).sendMail(new DeletedAssignment(
					EduShareSession.get().getLoggedInUser(), u, assgn).send());
		} else {
			new OutgoingEmailsDAO(EduShareSession.get()
					.getSessionORMInterface()).sendMail(new EditedAssignment(
					EduShareSession.get().getLoggedInUser(), u, assgn).send());
		}
	}
}
